package com.example.arwhack.util;

import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class HttpUtil {

    private final static String LOGIN_URL = "http://121.199.193.42:16888/login?username=%s&password=%s";

    private final static String TRANSACTION_RECORD = "http://121.199.193.42:16888/transactionRecord/all";

    public static void login(String userName, String password, Callback responseCallback) {
        OkHttpClient client = new OkHttpClient();
        String completeUrl = String.format(LOGIN_URL, userName, password);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), "");
        Request request = new Request.Builder().url(completeUrl).
                addHeader("Content-Type", "application/json")
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(responseCallback);

    }

    public static void getRecords(String token, String coin, String currency, Callback responseCallback) {
        OkHttpClient client = new OkHttpClient();
        String json = String.format("{\"virtualCoin\":\"%s\",\"currency\":\"%s\"}", coin, currency);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        Request request = new Request.Builder().url(TRANSACTION_RECORD)
                .addHeader("Content-Type", "application/json")
                .addHeader("Cookie", "Authorization="+token)
                .post(requestBody).build();
        client.newCall(request).enqueue(responseCallback);
    }

}
